package org.bookie.model.hibernate;

import org.bookie.model.Group;
import org.hibernate.event.PostLoadEvent;
import org.hibernate.event.PostLoadEventListener;

public class GroupPostLoadEventListener implements PostLoadEventListener {
	private static final long serialVersionUID = 1L;
	
	public void onPostLoad(PostLoadEvent event) {
		Object entity = event.getEntity();
		if(!entity.getClass().equals(Group.class)){
			return;
		}
		Group group = (Group)entity;
		Number number = (Number) event.getSession()
					.createFilter(group.getUsers(), "select count(*)").list().get(0);
		group.setUserCount(number.intValue());

	}

}
